package msg

import (
	"github.com/lherman-cs/go-rosbag"
	"time"
)

var g_map_init_flag bool
var G_map_serialize map[string]func(funcData *FucData)

type FucData struct {
	Topic         string
	Record        *rosbag.RecordMessageData
	Callback      func(data TopicData)
	ErrorCallback func(topic string, err error)
}

type TopicData struct {
	Topic   string
	MsgType string
	Data    interface{}
}

func SerializeCommonData(funcData *FucData) {
	data := make(map[string]interface{})
	err := funcData.Record.ViewAs(data)
	if err != nil {
		funcData.ErrorCallback(funcData.Topic, err)
		return
	}
	//str := fmt.Sprintf("%v", data)
	//bytes, err := json.Marshal(data)
	//if err != nil {
	//	funcData.ErrorCallback(funcData.Topic, err)
	//	return
	//}
	funcData.Callback(TopicData{
		Topic:   funcData.Topic,
		MsgType: funcData.Record.ConnectionHeader().Type,
		Data:    data,
	})
}

func Init() {
	if g_map_init_flag {
		return
	}
	G_map_serialize = make(map[string]func(funcData *FucData))
	g_map_init_flag = true

	G_map_serialize["/serial_com_driver/odom"] = SerializeCommonData
	G_map_serialize["/serial_com_driver/software_version"] = SerializeCommonData
	G_map_serialize["/serial_com_driver/imu"] = SerializeCommonData
	G_map_serialize["/scan"] = SerializeLaserScan
	G_map_serialize["/slam/scan_match_points"] = SerializeCommonData
	G_map_serialize["/edge_laser_info"] = SerializeCommonData
	G_map_serialize["/slam/grid_map"] = SerializeCommonData
	G_map_serialize["/map_manage/hmi_map"] = SerializeCommonData
	G_map_serialize["/map_manage/hmi_viz_map"] = SerializeCommonData
	G_map_serialize["/local_map/polar_nearest_point"] = SerializeCommonData
	G_map_serialize["/local_map/line"] = SerializeCommonData
	G_map_serialize["/local_map/chairs"] = SerializeCommonData
	G_map_serialize["/local_map/chair_groups"] = SerializeCommonData
	G_map_serialize["/global_map"] = SerializeCommonData
	G_map_serialize["/room_map"] = SerializeCommonData
	G_map_serialize["/slam/track_pose"] = SerializeCommonData
	G_map_serialize["/cost/base_station"] = SerializeCommonData
	G_map_serialize["/slam/base_station"] = SerializeCommonData
	G_map_serialize["/left_linelaser_cloud"] = SerializeCommonData
	G_map_serialize["/right_linelaser_cloud"] = SerializeCommonData
	G_map_serialize["/left_linelaser_ground"] = SerializeCommonData
	G_map_serialize["/right_linelaser_ground"] = SerializeCommonData
	G_map_serialize["/left_linelaser_cloud_calib"] = SerializeCommonData
	G_map_serialize["/right_linelaser_cloud_calib"] = SerializeCommonData
	G_map_serialize["/perception/semantic_map"] = SerializeCommonData
	G_map_serialize["/perception/ai_obstacle_markers"] = SerializeCommonData
	G_map_serialize["/perception/ai_fov_marker"] = SerializeCommonData
	G_map_serialize["/walkalong_chain_pose_marker"] = SerializeCommonData
	G_map_serialize["/wall_follow_trajectory"] = SerializeCommonData
	G_map_serialize["/explore_frontier"] = SerializeCommonData
	G_map_serialize["/boolrobotmain/global_planner/plan"] = SerializeCommonData
	G_map_serialize["/base_station_signal"] = SerializeCommonData
}

type Header struct {
	Seq     uint32    `rosbag:"seq"`
	Stamp   time.Time `rosbag:"stamp"`
	FrameId string    `rosbag:"frame_id"`
}
